package fr.upstlse.glre.client.espacededie.commun.utilisateur;

import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.user.client.ui.Label;

import fr.upstlse.glre.client.constante.RfgConstanteEnum;
import fr.upstlse.glre.client.widgets.modelData.CriteriaModelData;

public abstract class AbstractFilterBar extends LayoutContainer {

	protected ComboBox<CriteriaModelData> criteriaField;
	protected TextField<String> criteriaValue;
	protected Button rechercheButton;
	protected Button supprimerFiltre;
	protected Label descriptionFiltre;
	
	protected AbstractFilterAction filterAction;
	protected AbstractRemoveFilterAction removeFilterAction;
	
	protected static AbstractFilterBar instance;
	
	protected AbstractFilterBar() {
		criteriaField = createCriteriaField();
		criteriaValue = createCriteriaValue();
		createComponent();
		instance = this;
	}
	
	protected void createComponent(){
		if(criteriaField != null) {
			add(criteriaField);
		}
		if(criteriaValue != null) {
			add(criteriaValue);
		}
		
		rechercheButton = new Button();
		rechercheButton.setText(RfgConstanteEnum.LISTERUSER_FILTER_SEARCH_BUTTON_TEXT.getValue());
		rechercheButton.setId(RfgConstanteEnum.LISTERUSER_FILTER_SEARCH_BUTTON_TEXT.getValue());
		
		add(rechercheButton);
		
		supprimerFiltre = new Button();
		supprimerFiltre.setText(RfgConstanteEnum.LISTERUSER_FILTER_CLEAR_SEARCH_BUTTON_TEXT.getValue());
		supprimerFiltre.setId(RfgConstanteEnum.LISTERUSER_FILTER_CLEAR_SEARCH_BUTTON_ID.getValue());
		add(supprimerFiltre);
		
		descriptionFiltre = new Label();
		descriptionFiltre.setText(RfgConstanteEnum.LISTEUSER_FILTER_DESCRIPTION_LABEL_TEXT.getValue());
		descriptionFiltre.setStyleName("filterDescription");
		add(descriptionFiltre);
		
		
	}
	
	protected abstract ComboBox<CriteriaModelData> createCriteriaField();
	
	protected abstract TextField<String> createCriteriaValue();
	
	public ComboBox<CriteriaModelData> getCriteriaField() {
		return criteriaField;
	}

	public void setCriteriaField(ComboBox<CriteriaModelData> criteriaField) {
		this.criteriaField = criteriaField;
	}

	public TextField<String> getCriteriaValue() {
		return criteriaValue;
	}

	public void setCriteriaValue(TextField<String> criteriaValue) {
		this.criteriaValue = criteriaValue;
	}

	public AbstractFilterAction getFilterAction() {
		return filterAction;
	}

	public void setFilterAction(AbstractFilterAction filterAction) {
		this.filterAction = filterAction;
		rechercheButton.addSelectionListener(filterAction);
		
	}

	public static AbstractFilterBar getInstance() {
		return instance;
	}

	public static void setInstance(AbstractFilterBar instance) {
		AbstractFilterBar.instance = instance;
	}
	
	public void setSupprimerFilterAction(AbstractRemoveFilterAction pRemoveFilterAction) {
		removeFilterAction = pRemoveFilterAction;
		supprimerFiltre.addSelectionListener(removeFilterAction);
	}
}
